package com.whnk.model.controller;

import com.alibaba.fastjson.JSONObject;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.common.core.web.page.TableDataInfo;
import com.whnk.common.log.annotation.Log;
import com.whnk.common.log.enums.BusinessType;
import com.whnk.common.security.annotation.PreAuthorize;
import com.whnk.model.domain.DefTCustinfo;
import com.whnk.model.domain.InterServices;
import com.whnk.model.service.IDefTCustinfoService;
import com.whnk.model.service.IInterServicesService;
import com.whnk.model.tool.HttpClientToInterface;
import com.whnk.model.tool.JsonTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 接口服务Controller
 *
 * @author whnk
 * @date 2021-11-19
 */
@Api(tags = "接口服务")
@RestController
@RequestMapping("/services")
public class InterServicesController extends BaseController
{
    @Autowired
    private IInterServicesService interServicesService;

    @Autowired
    private IDefTCustinfoService defTCustinfoService;

    @ApiOperation(value = "请求分发")
    @Log(title = "接口服务-请求分发", businessType = BusinessType.INSERT)
    @PostMapping("/reqDis")
    public AjaxResult reqDis(@RequestBody InterServices interServices) throws UnsupportedEncodingException
    {
        String reqMessage = interServices.getReqMessage();
        if(!JsonTool.isJson(reqMessage)){
            return AjaxResult.error("报文格式不对！！");
        }
        JSONObject jsonReq = JSONObject.parseObject(reqMessage);
        //校验接入系统是否正常
        DefTCustinfo defTCustinfo = new DefTCustinfo();
        defTCustinfo.setCustName(jsonReq.get("sysName")+"");
        defTCustinfo.setDataSourceNotice(jsonReq.get("sysPw")+"");
        defTCustinfo.setStatus("1");
        List<DefTCustinfo> list = defTCustinfoService.selectDefTCustinfoList(defTCustinfo);
        if(list.size()==0){
            return AjaxResult.error("接入系统错误！！");
        }
        String result="";
        if("get".equals(interServices.getReqWay().toLowerCase())){
            result=HttpClientToInterface.doGet(interServices.getReqUrl(),"UTF-8");
        }
        else {
             result=HttpClientToInterface.doPost(interServices.getReqUrl(),interServices.getReqMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询接口服务列表
     */
    @ApiOperation(value = "查询录入的接口列表")
    @PreAuthorize(hasPermi = "model:services:list")
    @GetMapping("/list")
    public TableDataInfo list(InterServices interServices)
    {
        startPage();
        List<InterServices> list = interServicesService.selectInterServicesList(interServices);
        return getDataTable(list);
    }

    /**
     * 导出接口服务列表
     */
//    @PreAuthorize(hasPermi = "model:services:export")
//    @Log(title = "接口服务", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, InterServices interServices) throws IOException
//    {
//        List<InterServices> list = interServicesService.selectInterServicesList(interServices);
//        ExcelUtil<InterServices> util = new ExcelUtil<InterServices>(InterServices.class);
//        util.exportExcel(response, list, "接口服务数据");
//    }

    /**
     * 获取接口服务详细信息
     */
    @ApiOperation(value = "查询指定的接口列表")
    @PreAuthorize(hasPermi = "model:services:query")
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable("tableId") String tableId)
    {
        return AjaxResult.success(interServicesService.selectInterServicesByTableId(tableId));
    }

    /**
     * 新增接口服务
     */
    @ApiOperation(value = "录入接口")
    @PreAuthorize(hasPermi = "model:services:add")
    @Log(title = "接口服务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody InterServices interServices)
    {
        return toAjax(interServicesService.insertInterServices(interServices));
    }

    /**
     * 修改接口服务
     */
    @ApiOperation(value = "修改接口")
    @PreAuthorize(hasPermi = "model:services:edit")
    @Log(title = "接口服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InterServices interServices)
    {
        return toAjax(interServicesService.updateInterServices(interServices));
    }

    /**
     * 删除接口服务
     */
    @ApiOperation(value = "删除接口")
    @PreAuthorize(hasPermi = "model:services:remove")
    @Log(title = "接口服务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tableIds}")
    public AjaxResult remove(@PathVariable String[] tableIds)
    {
        return toAjax(interServicesService.deleteInterServicesByTableIds(tableIds));
    }
}
